<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function test(){//
		$mac=I('');
		$data2 = var_export($mac,true);
		file_put_contents(date('Ymd-His')."006".".txt",$data2);die;
	
        //print_r($_SERVER);die;
        $return=SendMail('1374278634@qq.com', '测试', '测试');
        print_r($return);
    }
	
    public function index(){
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        //banner
        $banner=M('rolls')->field('id,url,link')->where(['status'=>1])->order('id desc')->select();
        $this->assign('banner',$banner);

        //交易软件
        $software=M('software')->field('software,plug_ins')->order('id desc')->find();
        $this->assign('software',$software);

        /*$rose = M('rose')->order('id desc')->where(['type'=>1])->select();
        //$rose_sum = M('rose')->order(0,11)->sum('rose');
        $this->assign('rose',$rose);*/

        //$aeronautical = M('rose')->order('id desc')->where(['type'=>1])->select();
        /*$aeronautical=M('aeronautical')->field('name,code,address')->order('id desc')->limit(10)->select();*/
        //$this->assign('aeronautical',$aeronautical);
        //$this->assign('rose_sum',$rose_sum);

        $articleModel=M('article');
        $news['notice']=$articleModel->field('id,title,img,add_time')->where(['type'=>1])->order('id desc')->find();//公告
        $news['information']=$articleModel->field('id,title,img,add_time')->where(['type'=>2])->order('id desc')->find();//资讯
        $news['news']=$articleModel->field('id,title,img,add_time')->where(['type'=>3])->order('id desc')->find();//新闻
        $this->assign('news',$news);

        $this->assign('class','index');
        $this->assign('title','国富金融');
        $this->display();
    }

    public function about(){//公司介绍
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $msg=M('about')->field('content')->find();
        $this->assign('msg',$msg);
        $this->assign('class','about');
        $this->assign('title','公司介绍');
        $this->display();
    }

    public function product(){//产品列表
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->assign('class','product');
        $this->assign('title','产品列表');
        $this->display();
    }

    public function download(){//软件下载
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $software=M('software')->field('software,plug_ins')->order('id desc')->find();
        $this->assign('software',$software);
        $this->assign('class','download');
        $this->assign('title','软件下载');
        $this->display();
    }

    public function guide(){//交易指南
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->assign('class','guide');
        $this->assign('title','交易指南');
        $this->display();
    }

    public function goodness(){//集团优势
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->assign('class','goodness');
        $this->assign('title','集团优势');
        $this->display();
    }

    public function partner(){//合作伙伴
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->assign('class','partner');
        $this->assign('title','合作伙伴');
        $this->display();
    }

    public function newsIndex(){//新闻公告
        $type=I('type') ? I('type') : 0;
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);

        $model = M('article');
        if(empty($type)){
            $count = $model->count();
        }else{
            $count = $model->where(['type'=>$type])->count();
        }
        $Page = new \Think\Page($count,6);
        $Page->parameter['type']=$type;
        $show = $Page->show();
        if(empty($type)){
            $list = $model->field('id,title,add_time,img')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        }else{
            $list = $model->field('id,title,add_time,img')->where(['type'=>$type])->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        }
        $this->assign('count',$count);
        $this->assign('list',$list);
        $this->assign('page',$show);

        $this->assign('class','newsIndex');
        $this->assign('title','新闻公告');
        $this->display();
    }

    public function newsShow(){
        $id=I('id');
        $model=M('article');
        $msg=$model->where(['id'=>$id])->find();
        $prev=$model->field('id,title')->where('id > '.$id)->find();
        $next=$model->field('id,title')->where('id < '.$id)->order('id desc')->find();
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->assign('class','newsIndex');
        $this->assign('title','新闻公告-详情');
        $this->assign('msg',$msg);
        $this->assign('prev',$prev);
        $this->assign('next',$next);
        $this->display();
    }

    public function loginView(){
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->display();
    }

    public function commonTop(){
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->display();
    }

    public function commonBottom(){
        $this->display();
    }

    public function login(){//首页登录
        $account=I('post.account') ? I('post.account') : die(json_encode(['status'=>0,'msg'=>'请输入登陆邮箱或mt4账号']));
        $pwd=I('post.pwd') ? I('post.pwd') : die(json_encode(['status'=>0,'msg'=>'请输入密码']));
        $pwd=md5(md5($pwd));
        $res=M('user')->where('(user_email=\''.$account.'\') or (user_id=\''.$account.'\')')->find();
        if(empty($res)){
            $this->ajaxReturn(array('status'=>0,'msg'=>'账号不存在'));
        }else if($res['user_password']!=$pwd){
            $this->ajaxReturn(array('status'=>0,'msg'=>'密码不正确'));
        }else{
			 if($res['user_state']==2){
				   $this->ajaxReturn(array('status'=>0,'msg'=>'账号已被禁用，请联系管理员'));      
			 }	
            if($res['user_role']==1){
                session('user',$res);
                $this->ajaxReturn(array('status'=>1,'msg'=>U('User/Index/index')));
            }else if($res['user_role']==3){
                session('report',$res);
                $this->ajaxReturn(array('status'=>1,'msg'=>U('Report/Index/index')));
            }else{
                session('admin',$res);
                $this->ajaxReturn(array('status'=>1,'msg'=>U('Admin/Index/index')));
            }

        }
    }


    public function downloadData(){//导出用户资料
        $user=M('user')->field('user_id,user_name,name_zh_cn,user_cards,user_email,user_phone,user_address,group,province,city,zipcode,country,user_birthday,front_img,behind_img,hand_img,weixin')->where(['status'=>3,'user_role'=>1])->select();
        foreach ($user as $k => $v){
            $path="./Uploads/userdata/".$v['user_id'];
            if (!file_exists($path)){
                $res=mkdir($path);
                if($res){
                    $str='MT4 : '.$v['user_id'].PHP_EOL.'姓名（英文） : '.$v['user_name'].PHP_EOL.'姓名（中文） : '.$v['name_zh_cn'].PHP_EOL.'身份证号 : '.$v['user_cards'].PHP_EOL.'邮箱 : '.$v['user_email'].PHP_EOL.'电话 : '.$v['user_phone'].PHP_EOL.'地址 : '.$v['user_address'].PHP_EOL.'组别 : '.$v['group'].PHP_EOL.'省 : '.$v['province'].PHP_EOL.'市 : '.$v['city'].PHP_EOL.'邮编 : '.$v['zipcode'].PHP_EOL.'国家 : '.$v['country'].PHP_EOL.'生日 : '.$v['user_birthday'].PHP_EOL.'微信 : '.$v['weixin'];
                    file_put_contents($path."/test.txt",$str);
                    if(file_exists($v['front_img'])){
                        copy($v['front_img'], $path .'/front.'.substr(strrchr($v['front_img'],'.'),1));
                    }
                    if(file_exists($v['behind_img'])){
                        copy($v['behind_img'], $path .'/behind.'.substr(strrchr($v['behind_img'],'.'),1));
                    }
                    if(file_exists($v['hand_img'])){
                        copy($v['hand_img'], $path .'/hand.'.substr(strrchr($v['hand_img'],'.'),1));
                    }
                }else{
                    echo $v['user_id'].'<br/>';
                }
            }else{
                $str='MT4 : '.$v['user_id'].PHP_EOL.'姓名（英文） : '.$v['user_name'].PHP_EOL.'姓名（中文） : '.$v['name_zh_cn'].PHP_EOL.'身份证号 : '.$v['user_cards'].PHP_EOL.'邮箱 : '.$v['user_email'].PHP_EOL.'电话 : '.$v['user_phone'].PHP_EOL.'地址 : '.$v['user_address'].PHP_EOL.'组别 : '.$v['group'].PHP_EOL.'省 : '.$v['province'].PHP_EOL.'市 : '.$v['city'].PHP_EOL.'邮编 : '.$v['zipcode'].PHP_EOL.'国家 : '.$v['country'].PHP_EOL.'生日 : '.$v['user_birthday'].PHP_EOL.'微信 : '.$v['weixin'];
                file_put_contents($path."/test.txt",$str);
                if(file_exists($v['front_img'])){
                    copy($v['front_img'], $path .'/front.'.substr(strrchr($v['front_img'],'.'),1));
                }
                if(file_exists($v['behind_img'])){
                    copy($v['behind_img'], $path .'/behind.'.substr(strrchr($v['behind_img'],'.'),1));
                }
                if(file_exists($v['hand_img'])){
                    copy($v['hand_img'], $path .'/hand.'.substr(strrchr($v['hand_img'],'.'),1));
                }
            }
        }
    }

    public function register(){//注册
        $logo=M('logo')->field('id,logo,ico,crm_logo,web_name_en')->find();
        $this->assign('logo',$logo);
        $switch=M('register_switch')->find();
        $this->assign('switch',$switch);
        if(empty($switch['binary']) && empty($switch['foreign'])){
            exit('注册通道处于关闭中.....，<a href="javascript:history.back(-1)">点击返回</a>');
        }
        $id=I('agent') ? I('agent') : 0;
        $this->assign('agent',$id);
        $this->display();
    }

    public function Verify(){
        ob_clean();
        $Verify = new \Think\Verify();
        $Verify->fontSize = 18;
        $Verify->length   = 4;
        $Verify->useNoise = false;
        $Verify->entry();
    }

    public function register_do(){//注册
        $user_date['user_name'] = I('post.user_name') ? I('post.user_name') : die('error');
        $user_date['name_zh_cn'] = I('post.name_zh_cn') ? I('post.name_zh_cn') : die('error');
        $user_date['user_phone'] = I('post.user_phone') ? I('post.user_phone') : die('error');
        $user_date['sex'] = I('post.sex') ? I('post.sex') : die('error');
        $user_date['user_email'] = I('post.user_email') ? I('post.user_email') : die('error');
        $user_date['card_type'] = I('post.card_type') ? I('post.card_type') : die('error');
        $user_date['user_cards'] = I('post.user_cards') ? I('post.user_cards') : die('error');
        $user_date['user_address'] = I('post.user_address') ? I('post.user_address') : die('error');
        $user_date['user_birthday'] = I('post.user_birthday') ? I('post.user_birthday') : die('error');
        $user_date['user_recommend'] = I('post.user_belong') ? I('post.user_belong') : 0;
        $pwd = I('post.password') ? I('post.password') : die('error1');
        $repwd = I('post.repwd') ? I('post.repwd') : die('error2');
        if($pwd!=$repwd){
            $this->ajaxReturn(array("msg"=>"确认密码不一致","status"=>0));
        }else{
            $user_date['user_password'] = md5(md5($pwd));
        }
        $user_date['image'] = 'Uploads/img.jpg';//头像
        $user_date['user_role'] = 1;
        $user_date['user_state'] = I('post.enable') ? I('post.enable') : 1;//是否启用账户
        $user_date['user_createtime'] = date('Y-m-d H:i:s');
        $user_date['user_lastlogin'] = 0;
        $verification = I('post.verification') ? I('post.verification') : die('error3');

        $user_date['province'] = I('post.province') ? I('post.province') : '';
        $user_date['city'] = I('post.city') ? I('post.city') : '';
        $user_date['proportion'] = $user_date['level'] = I('post.proportion') ? I('post.proportion') : 100;//杠杆
        $user_date['zipcode'] = I('post.zipcode') ? I('post.zipcode') : '610000';
        $user_date['country'] = I('post.country') ? I('post.country') : 'China';
        $user_date['password_phone'] = I('post.password_phone') ? I('post.password_phone') : md5(md5($pwd));
        $user_date['password_investor'] = I('post.password_investor') ? I('post.password_investor') : md5(md5($pwd));//投资者密码
        $logo=M('logo')->field('web_name_en')->find();
        $user_date['comment'] = I('post.comment') ? I('post.comment') : $logo['web_name_en'];//备注
        //$user_date['enable'] = I('post.enable') ? I('post.enable') : 1;//是否启用账户
        $user_date['enable_change_password'] = I('post.enable_change_password') ? I('post.enable_change_password') : 1;
        $user_date['enable_read_only'] = I('post.enable_read_only') ? I('post.enable_read_only') : 0;
        $user_date['send_reports'] = I('post.send_reports') ? I('post.send_reports') : 1;//是否发送报告
        $user_date['status'] = I('post.status') ? I('post.status') : 1;//1--待审核，2-已申请，3--已核实，4--拒绝
        $user_date['front_img'] = I('post.front_img') ? I('post.front_img') : '';
        $user_date['behind_img'] = I('post.behind_img') ? I('post.behind_img') : '';
        $user_date['hand_img'] = I('post.hand_img') ? I('post.hand_img') : '';
        $user_date['position'] = I('post.position') ? I('post.position') : 1;
        $user_date['img_apply_time'] = I('post.img_apply_time') ? I('post.img_apply_time') : '';
        $user_date['commission'] = I('post.commission') ? I('post.commission') : 1;
        $user_date['account_type'] = I('post.account_type') ? I('post.account_type') : 1;
        //$user_date['level'] = I('post.level') ? I('post.level') : 1;
        $user_date['frozen_balance'] = I('post.frozen_balance') ? I('post.frozen_balance') : 0;
        $user_date['weixin'] = I('post.weixin') ? I('post.weixin') : '';
        $user_date['user_qq'] = I('post.user_qq') ? I('post.user_qq') : '';
        $user_date['type']=$type = I('post.type') ? I('post.type') : 2;//1短期 2 外汇
        $verify = new \Think\Verify();
        if($verify->check($verification)){
            $model=M('user');
            if(empty($user_date['user_recommend'])){
                /*if($type==2){
                    $user_date['group'] = 'MXVTEST';
                }else{
                    $user_date['group'] = 'TEST';
                }*/
                $user_date['group'] = 'HIJTEST';
            }else{
                $user_recommend=$model->field('group')->where(['user_id'=>$user_date['user_recommend']])->find();
                if(empty($user_recommend)){
                    $this->ajaxReturn(array("msg"=>"推荐号有误","status"=>0));
                }else{//判断是填写的代理人否是外汇组
                    $group_type=M('mt4_groups')->where(['mt4_group'=>$user_recommend['group']])->find();
                    if($type!=$group_type['type']){
                        $this->ajaxReturn(array("msg"=>"推荐码不正确","status"=>0));
                    }
                    $user_date['group'] = $user_recommend['group'];
                }
            }
            $has_user=$model->where(['user_email'=>$user_date['user_email']])->find();
            $has_user2=$model->where(['user_cards'=>$user_date['user_cards']])->find();
            if(!empty($has_user2)){
                $this->ajaxReturn(array("msg"=>"该身份证已注册","status"=>0));
            }
            if(!empty($has_user)){
                $this->ajaxReturn(array("msg"=>"该邮箱已注册","status"=>0));
            }else{
                $model->startTrans();
                $res=$model->add($user_date);
                if($res){
                    $request="action=createaccount&login=".$res."&agent=".$user_date['user_recommend']."&leverage=".$user_date['proportion']."&name=".$user_date['user_name']."&group=".$user_date['group']."&email=".$user_date['user_email']."&id=".$user_date['user_cards']."&address=".$user_date['user_address']."&city=".$user_date['city']."&state=".$user_date['province']."&zipcode=".$user_date['zipcode']."&country=".$user_date['country']."&phone=".$user_date['user_phone']."&password=".$pwd."&password_investor=".$user_date['password_investor']."&comment=".$user_date['comment']."&enable=".$user_date['user_state']."&enable_change_password=".$user_date['enable_change_password']."&enable_read_only=".$user_date['enable_read_only']."&send_reports=".$user_date['send_reports']."&status=".$user_date['status']."\0";
                    $mt4_register=mt4_operation($request);
                    parse_str($mt4_register, $mt4_register_status);
                    if($mt4_register_status['result']==1){
                        $model->commit();
						$email = M('mail_mould')->where(array('type'=>10001))->find();
						$message =str_replace('{1}',$res,$email['content']);
						$message =str_replace('{2}',$pwd,$message);
						$return = SendMail($user_date['user_email'], $email['title'], htmlspecialchars_decode($message));
						//$webName=M('logo')->field('web_name_en')->find();
                        //$return=@SendMail($user_date['user_email'],'注册成功', '恭喜您已在'.$webName['web_name_en'].'注册账号成功,<br/>您的MT4登录账号为：'.$res.',密码：'.$pwd.',请妥善保管好您的信息！！！');
                        $this->ajaxReturn(array("msg"=>"操作成功","status"=>1));
                    }else{
                        $model->rollback();
                        $mt4_register_str='';
                        switch ($mt4_register_status['result']){
                            case 0:
                                $mt4_register_str='invalid action type';
                                break;
                            case -1:
                                $mt4_register_str='mt4 connection issue';
                                break;
                            case -2:
                                $mt4_register_str='invalid hash (if hash used)';
                                break;
                            case -3:
                                $mt4_register_str='invalid incoming parameters';
                                break;
                            case -4:
                                $mt4_register_str='no any valid data on mt4 server';
                                break;
                            default:
                                $mt4_register_str='未知错误';
                        }
                        $this->ajaxReturn(array("msg"=>'mt注册失败 : '.$mt4_register_str,"status"=>0));
                    }
                }else{
                    $this->ajaxReturn(array("msg"=>"操作失败","status"=>0));
                }
            }
        }else{
            $this->ajaxReturn(array("msg"=>"验证码有误","status"=>0));
        }
    }

    public function leavingMessage(){//留言
        $data['book_name'] = I('post.name') ? I('post.name') : die(json_encode(array("msg"=>"请输入您的姓名","status"=>0)));
        $data['book_phone'] = '';//I('post.phone') ? I('post.phone') : die(json_encode(array("msg"=>"请输入您的联系电话","status"=>0)));
        $data['book_email'] = I('post.email') ? I('post.email') : die(json_encode(array("msg"=>"请输入您的邮箱","status"=>0)));
        $data['book_content'] = I('post.content') ? I('post.content') : die(json_encode(array("msg"=>"请输入您的留言内容","status"=>0)));
        $data['add_time'] = time();
        $data['status'] = 0;
        $data['remark'] = '';
        $res=M('book')->add($data);
        if($res){
            $this->ajaxReturn(array("msg"=>"留言成功","status"=>1));
        }else{
            $this->ajaxReturn(array("msg"=>"留言失败","status"=>0));
        }
    }

    public function agentApply(){//经纪人申请
        $data['user_id'] = I('post.agent_login') ? I('post.agent_login') : die(json_encode(array("msg"=>"请输入您的MT4账号","status"=>0)));
        $has=M('user')->where(['user_id'=>$data['user_id']])->find();
        if(empty($has)){
            $this->ajaxReturn(array("msg"=>"MT4账号有误","status"=>0));
        }
        $data['type'] = 4;
        $data['title'] = '经纪人申请';
        $data['describe'] = I('post.agent_address') ? '所在地区：'.I('post.agent_address') : die(json_encode(array("msg"=>"请输入您的地区","status"=>0)));
        $data['file2'] = '';
        $data['file3'] = '';
        $data['file'] = '';
        $data['phone'] = '';
        $data['email'] = I('post.agent_email') ? I('post.agent_email') : die(json_encode(array("msg"=>"请输入您的邮箱","status"=>0)));
        $data['time'] = time();
        $data['state'] = 1;
        $res=M('work_order')->add($data);
        if($res){
            $this->ajaxReturn(array("msg"=>"申请已提交，请您耐心等待...","status"=>1));
        }else{
            $this->ajaxReturn(array("msg"=>"申请提交失败","status"=>0));
        }
    }

    public function proxyApply(){//代理人申请
        $data['user_id'] = I('post.proxy_login') ? I('post.proxy_login') : die(json_encode(array("msg"=>"请输入您的MT4账号","status"=>0)));
        $has=M('user')->where(['user_id'=>$data['user_id']])->find();
        if(empty($has)){
            $this->ajaxReturn(array("msg"=>"MT4账号有误","status"=>0));
        }
        $data['type'] = 4;
        $data['title'] = '代理人申请';
        $data['describe'] = I('post.proxy_address') ? '所在地区：'.I('post.proxy_address') : die(json_encode(array("msg"=>"请输入您的地区","status"=>0)));
        $data['file2'] = '';
        $data['file3'] = '';
        $data['file'] = '';
        $data['phone'] = '';
        $data['email'] = I('post.proxy_email') ? I('post.proxy_email') : die(json_encode(array("msg"=>"请输入您的邮箱","status"=>0)));
        $data['time'] = time();
        $data['state'] = 1;
        $res=M('work_order')->add($data);
        if($res){
            $this->ajaxReturn(array("msg"=>"申请已提交，请您耐心等待...","status"=>1));
        }else{
            $this->ajaxReturn(array("msg"=>"申请提交失败","status"=>0));
        }
    }

}